﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Instrument;
using DevExpress.XtraBars.Docking2010;
using System.Threading;

namespace UI
{
    public partial class ChannelChoice : DevExpress.XtraEditors.XtraForm
    {
        int ChannelCount = 0;
        int ChannelUplimit = 0;
        public ChannelChoice(Production production)
        {
            InitializeComponent();
            ChannelUplimit = production.ResistanceCount;
            DevExpress.XtraBars.Docking2010.WindowsUIButton button = (DevExpress.XtraBars.Docking2010.WindowsUIButton)windowsUIButtonPanel1.Buttons[0];
            button.Tag = false;
        }

        private void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            if (e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void windowsUIButtonPanel2_ButtonClick(object sender, ButtonEventArgs e)
        {
            if(e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void windowsUIButtonPanel3_ButtonClick(object sender, ButtonEventArgs e)
        {
            if (e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void windowsUIButtonPanel5_ButtonClick(object sender, ButtonEventArgs e)
        {
            if (e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void windowsUIButtonPanel4_ButtonClick(object sender, ButtonEventArgs e)
        {
            if (e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void windowsUIButtonPanel6_ButtonClick(object sender, ButtonEventArgs e)
        {
            if (e.Button is WindowsUIButton)
            {
                WindowsUIButton button = (WindowsUIButton)e.Button;
                if ((bool)button.Tag == false)
                {
                    ChannelCount += 1;
                    if (ChannelCount > ChannelUplimit)
                    {
                        ChannelCount--;
                        MessageBox.Show("通道数已达上限");
                    }
                    else
                    {
                        button.Tag = true;
                        button.Image = Properties.Resources.ChannelChecked;
                    }
                }
                else
                {
                    button.Tag = false;
                    ChannelCount--;
                    button.Image = null;
                }
            }
        }

        private void ChannelChoice_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(ChannelCount < ChannelUplimit)
            {
                DialogResult dialogResult = MessageBox.Show("通道配置与产品规格不符，点击“确认”退出当前界面，并使用默认通道，点击“取消”，返回界面重新配置"
                    , "提示", MessageBoxButtons.OKCancel);
                if(dialogResult == DialogResult.OK)
                {
                    return;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach(var item in windowsUIButtonPanel1.Buttons)
                {
                    if(item is WindowsUIButton)
                    {
                        if((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                foreach (var item in windowsUIButtonPanel2.Buttons)
                {
                    if (item is WindowsUIButton)
                    {
                        if ((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                foreach (var item in windowsUIButtonPanel3.Buttons)
                {
                    if (item is WindowsUIButton)
                    {
                        if ((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                foreach (var item in windowsUIButtonPanel5.Buttons)
                {
                    if (item is WindowsUIButton)
                    {
                        if ((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                foreach (var item in windowsUIButtonPanel4.Buttons)
                {
                    if(item is WindowsUIButton)
                    {
                        if ((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                foreach (var item in windowsUIButtonPanel6.Buttons)
                {
                    if (item is WindowsUIButton)
                    {
                        if ((bool)((WindowsUIButton)item).Tag)
                            stringBuilder.Append(((WindowsUIButton)item).Caption + ",");
                    }
                }
                MainForm._C34970AConfiger.ChannelNumGroup = stringBuilder.Remove(stringBuilder.Length - 1, 1).ToString();
            }
        }
    }
}